package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.mapper.CheckgroupMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.*;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Locale;

/**
 * @author cfr
 * @date 2022/1/6
 * @TIME:19:49
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public Object findById(Integer id) {

        Setmeal setmeal = setmealMapper.selectById(id);
        Integer[] checkgroupids = setmealMapper.list(id);
        ArrayList<CheckGroup> checkGroups = new ArrayList<>();
        for (Integer checkgroupid : checkgroupids) {
            CheckGroup checkGroup = checkgroupMapper.selectById(checkgroupid);
            Integer[] checkItemids = setmealMapper.list2(checkGroup.getId());
            ArrayList<CheckItem> checkItems = new ArrayList<>();
            for (Integer checkItemid : checkItemids) {
                CheckItem checkItem = checkItemMapper.selectById(checkItemid);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
            checkGroups.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroups);

        return setmeal;
    }
}